/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManConstante.java
 *
 * Created on 25/03/2011, 04:11:13 PM
 */

package com.catosist.run.application.constante;

import com.catosist.run.business.gestorConstante.GestorConstante;
import com.catosist.run.services.ConstanteDTO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author igorov
 */
public class ManConstante extends javax.swing.JFrame {

    /** Creates new form ManConstante */
    public ManConstante() {
        initComponents();
    }

    public ManConstante(GestorConstante gestorConstante) {
        initComponents();

        this.gestorConstante = gestorConstante;
        llenarIGV();
        llenarDetraccion();
    }

    private void llenarIGV(){
        constanteIGV = gestorConstante.getById(igvId);
        Float igv = constanteIGV.getPorcentaje();
        txtIGV.setText(String.valueOf(igv * 100));
    }

    private void llenarDetraccion(){
        constanteDetraccion = gestorConstante.getById(detraccionId);
        Float detraccion = constanteDetraccion.getPorcentaje();
        txtDetraccion.setText(String.valueOf(detraccion * 100));
    }

    public JPanel getPanel(){
        return jPanel1;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtIGV = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDetraccion = new javax.swing.JTextField();
        chkModificarIGV = new javax.swing.JCheckBox();
        chkModificarDetraccion = new javax.swing.JCheckBox();
        btnAceptarIGV = new javax.swing.JButton();
        btnAceptarDetraccion = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(ManConstante.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtIGV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtIGV.setText(resourceMap.getString("txtIGV.text")); // NOI18N
        txtIGV.setEnabled(false);
        txtIGV.setName("txtIGV"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtDetraccion.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDetraccion.setEnabled(false);
        txtDetraccion.setName("txtDetraccion"); // NOI18N

        chkModificarIGV.setFont(resourceMap.getFont("chkModificarIGV.font")); // NOI18N
        chkModificarIGV.setText(resourceMap.getString("chkModificarIGV.text")); // NOI18N
        chkModificarIGV.setName("chkModificarIGV"); // NOI18N
        chkModificarIGV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkModificarIGVActionPerformed(evt);
            }
        });

        chkModificarDetraccion.setFont(resourceMap.getFont("chkModificarDetraccion.font")); // NOI18N
        chkModificarDetraccion.setText(resourceMap.getString("chkModificarDetraccion.text")); // NOI18N
        chkModificarDetraccion.setName("chkModificarDetraccion"); // NOI18N
        chkModificarDetraccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkModificarDetraccionActionPerformed(evt);
            }
        });

        btnAceptarIGV.setText(resourceMap.getString("btnAceptarIGV.text")); // NOI18N
        btnAceptarIGV.setEnabled(false);
        btnAceptarIGV.setName("btnAceptarIGV"); // NOI18N
        btnAceptarIGV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarIGVActionPerformed(evt);
            }
        });

        btnAceptarDetraccion.setText(resourceMap.getString("btnAceptarDetraccion.text")); // NOI18N
        btnAceptarDetraccion.setEnabled(false);
        btnAceptarDetraccion.setName("btnAceptarDetraccion"); // NOI18N
        btnAceptarDetraccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarDetraccionActionPerformed(evt);
            }
        });

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDetraccion)
                            .addComponent(txtIGV, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAceptarIGV)
                            .addComponent(btnAceptarDetraccion))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkModificarIGV)
                            .addComponent(chkModificarDetraccion))))
                .addContainerGap(476, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtIGV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(btnAceptarIGV)
                    .addComponent(chkModificarIGV))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDetraccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(btnAceptarDetraccion)
                    .addComponent(chkModificarDetraccion))
                .addContainerGap(331, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chkModificarIGVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkModificarIGVActionPerformed
        // TODO add your handling code here:
        txtIGV.setEnabled(chkModificarIGV.isSelected());
        btnAceptarIGV.setEnabled(chkModificarIGV.isSelected());
    }//GEN-LAST:event_chkModificarIGVActionPerformed

    private void chkModificarDetraccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkModificarDetraccionActionPerformed
        // TODO add your handling code here:
        txtDetraccion.setEnabled(chkModificarDetraccion.isSelected());
        btnAceptarDetraccion.setEnabled(chkModificarDetraccion.isSelected());
    }//GEN-LAST:event_chkModificarDetraccionActionPerformed

    private void btnAceptarIGVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarIGVActionPerformed
        // TODO add your handling code here:
        try {

            int result = JOptionPane.showConfirmDialog(this, "Realmente desea cambiar el IGV a " + txtIGV.getText() + "%?", "Cambiar IGV", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                Float porcentajeIGV = Float.valueOf(txtIGV.getText());
                porcentajeIGV /= 100;
                constanteIGV.setPorcentaje(porcentajeIGV);

                gestorConstante.saveOrUpdate(constanteIGV);
                JOptionPane.showMessageDialog(this, "El valor del IGV ha sido cambiado satisfactoriamente", "Cambio satisfactorio", JOptionPane.INFORMATION_MESSAGE);
            }
            else
                txtIGV.setText(String.valueOf(constanteIGV.getPorcentaje() * 100));
            
            chkModificarIGV.setSelected(false);
            txtIGV.setEnabled(false);
            btnAceptarIGV.setEnabled(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Dato incorrecto", "Advertencia", JOptionPane.WARNING_MESSAGE);
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_btnAceptarIGVActionPerformed

    private void btnAceptarDetraccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarDetraccionActionPerformed
        // TODO add your handling code here:
        try {
            int result = JOptionPane.showConfirmDialog(this, "Realmente desea cambiar la detraccion a " + txtDetraccion.getText() + "%?", "Cambiar detraccion", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                Float porcentajeDetraccion = Float.valueOf(txtDetraccion.getText());
                porcentajeDetraccion /= 100;
                constanteDetraccion.setPorcentaje(porcentajeDetraccion);

                gestorConstante.saveOrUpdate(constanteDetraccion);
                JOptionPane.showMessageDialog(this, "El valor de la Detraccion ha sido cambiado satisfactoriamente", "Cambio satisfactorio", JOptionPane.INFORMATION_MESSAGE);
            }

            else
                txtDetraccion.setText(String.valueOf(constanteDetraccion.getPorcentaje() * 100));
            
            chkModificarDetraccion.setSelected(false);
            txtDetraccion.setEnabled(false);
            btnAceptarDetraccion.setEnabled(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Dato incorrecto", "Advertencia", JOptionPane.WARNING_MESSAGE);
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_btnAceptarDetraccionActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManConstante().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptarDetraccion;
    private javax.swing.JButton btnAceptarIGV;
    private javax.swing.JCheckBox chkModificarDetraccion;
    private javax.swing.JCheckBox chkModificarIGV;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtDetraccion;
    private javax.swing.JTextField txtIGV;
    // End of variables declaration//GEN-END:variables
    private ConstanteDTO constanteIGV;
    private ConstanteDTO constanteDetraccion;
    private final int igvId = 1;
    private final int detraccionId = 2;
    private GestorConstante gestorConstante;
}
